import pygame,  Utilities,  Timer,  Level,  Balloon,  random,  Player,  Shoot,  Explosion,  Score,  Button
from pygame.locals import *
from Utilities import *
from Timer import *
from Level import *
from Balloon import *
from Player import *
from Shoot import *
from Explosion import *
from Score import *
from Button import *

BALLOON_SCORE = 0
SCORE = 0
PLAYING = 1
LEVEL_COUNT = 1
bgimg = None
bgimg2 = None
level = None
timer = None
score = None
ballon_score = None
timer_group = None
balloon_group = None
player_group = None
shoot_group = None
score_group = None
button_group = None
buttonPlay = None
buttonExit = None
player = None
all = None
surface_background = pygame.Surface
BACKGROUND_SIZE = Rect(0,  0,  810, 675)


def background(screen):
    global bgimg
    bgimg = load_image("board.jpg").convert()
    bgrect = bgimg.get_rect()
    screen.blit(bgimg,  bgrect)
    pygame.display.flip()
    
def init_gamescreen(screen,  level_number):
    global timer_group,  surface_background,  BACKGROUND_SIZE,  bgimg2
    background(screen)
    timer_group.draw(screen)
    surface_background = pygame.Surface(BACKGROUND_SIZE.size)
    bgimg2 =  load_image("wallpaper" + str(level_number % 5) + ".jpg")
    surface_background.blit(bgimg2,  (0, 0))
    screen.blit(surface_background,  (342, 38))
    pygame.display.flip()

def update_gamescreen(screen):
    global surface_background,  BACKGROUND_SIZE,  bgimg2
    surface_background = pygame.Surface(BACKGROUND_SIZE.size)
    bgimg2 =  load_image("burro.jpg")
    surface_background.blit(bgimg2,  (0, 0))
    screen.blit(surface_background,  (342, 38))
    pygame.display.flip()
    
def init_gamedata(level_number):
    global timer,timer_group,level,all,  balloon_group,  player_group,  player,  shoot_group,  score_group,  score,  ballon_score,  button_group,  buttonExit,  buttonPlay
    level = Level(level_number)
    timer = Timer(level.time)
    timer_group = pygame.sprite.RenderPlain(timer)
    balloon_group = pygame.sprite.RenderUpdates()
    shoot_group = pygame.sprite.RenderUpdates()
    player = Player(BACKGROUND_SIZE)
    score = Score((143, 700))
    ballon_score = Score((156, 235))
    score_group = pygame.sprite.RenderUpdates()
    score_group.add(score)
    score_group.add(ballon_score)
    player_group =pygame.sprite.RenderUpdates(player)
    button_group = pygame.sprite.RenderUpdates()
    buttonPlay = Button("New.jpg",  "NewSelected.jpg",  150, 345,  "play",  200,  30)
    buttonExit = Button("Exit.jpg",  "ExitSelected.jpg",  150, 410,  "exit",  200, 30)
    button_group.add(buttonPlay)
    button_group.add(buttonExit)

def timer_countdown(screen):
    global timer_group,  bgimg
    timer_group.clear(screen,  bgimg)
    timer_group.update()
    timer_group.draw(screen)
    pygame.display.flip()
    
def balloon_update(screen):
    global balloon_group,  bgimg2,  surface_background
    balloon_group.clear(surface_background,  bgimg2)
    balloon_group.update()
    balloon_group.draw(surface_background)
    screen.blit(surface_background,  (342, 38))
    pygame.display.flip()

def player_update(screen):
    global player_group,  surface_background,  bgimg2
    player_group.clear(surface_background,  bgimg2)
    player_group.update()
    player_group.draw(surface_background)
    screen.blit(surface_background,  (342, 38))
    pygame.display.flip()

def shoot_update(screen):
    global shoot_group,  surface_background,  bgimg2
    shoot_group.clear(surface_background,  bgimg2)
    shoot_group.update()
    shoot_group.draw(surface_background)
    screen.blit(surface_background,  (342, 38))
    pygame.display.flip

def score_update(screen):
    global score_group,  bgimg,  BALLOON_SCORE,  SCORE,  level,  score,  ballon_score
    score_group.clear(screen,  bgimg)
    score.text = str(SCORE)
    ballon_score.text = str(BALLOON_SCORE) + ' / ' + str(level.winning_score)
    score_group.update()
    score_group.draw(screen)
    pygame.display.flip()

def button_update(screen):
    global button_group,  buttonPlay,  buttonExit,  bgimg
    button_group.clear(screen,  bgimg)
    button_group.update()
    button_group.draw(screen)
    pygame.display.flip()

def main(screen):
    gameover = 0
    action = ""
    reset = 1
    global BALLOON_SCORE,  level,  all,  balloon_group,  player_group,  SCORE
    clock = pygame.time.Clock()
    explosion_images = load_images('explotion_1.bmp', 'explotion_2.bmp', 'explotion_3.bmp', 'explotion_4.bmp', 'explotion_5.bmp',  'explotion_6.bmp',  'explotion_7.bmp',  'explotion_8.bmp',  'explotion_9.bmp',  'explotion_10.bmp',  'explotion_11.bmp', 'explotion_12.bmp',  'explotion_13.bmp',  'explotion_14.bmp',  'explotion_15.bmp',  'explotion_16.bmp',  'explotion_17.bmp')
    while action != "exit":
        if reset:
            reset = 0
            level_number = 1
            gameover = 0
            action = ""
            SCORE = 0
            BALLOON_SCORE = 0
            
        init_gamedata(level_number)
        init_gamescreen(screen,  level_number)
        y_pos = 675
        pos = random.randint(0, 7)
        if (pos == 3) or (pos == 4):
            y_pos = 600
        pos = 50 +100*pos
        balloon = Balloon((pos, y_pos), level.dificulty,  level.balloons_speed)
        balloon_group.add(balloon)
        pygame.time.set_timer(pygame.USEREVENT + 1, level.interval)
        
        pygame.mixer.music.load(os.path.join('Data/Sound', "Kids_Music.wav"))
        pygame.mixer.music.play(-1)
        pygame.mixer.music.set_volume(0.1)
        
        while (BALLOON_SCORE < level.winning_score) and (not gameover) and (not reset):
            
            action = input(pygame.event.get(),  level_number)
            if action == "reset":
                pygame.mixer.music.stop()
                reset = 1
                action = ""
                break
            elif action == "exit":
                pygame.mixer.music.stop()
                break
            timer_countdown(screen)
            balloon_update(screen)
            player_update(screen)
            shoot_update(screen)
            score_update(screen)
            button_update(screen)
            if timer.timeover:
                gameover = 1
                update_gamescreen(screen)
                pygame.mixer.music.stop()
                pygame.mixer.music.load(os.path.join('Data/Sound', "donkey.mp3"))
                pygame.mixer.music.play()
                pygame.mixer.music.set_volume(0.05)
                actionEnd = ""
                while actionEnd == "" or actionEnd == "nothing":
                    actionEnd = input(pygame.event.get(),  level_number)
                    button_update(screen)
                if actionEnd == "exit":
                    return
                else:
                    reset = 1
                    action = ""
                    break
            else:
                for balloon in balloon_group:
                    for shoot in shoot_group:
                        if shoot.rect.colliderect(Rect(balloon.rect.left,  balloon.rect.top,  balloon.rect.width,  (balloon.rect.height * 0.60))):
                            # colisiona algun globo con los proyectiles
                            shoot.kill()
                            player_group.add(Explosion(balloon,  explosion_images))
                            boom_sound = load_sound("boom.wav")
                            boom_sound.play()
                            balloon.kill()
                            # el globo roto era erroneo, se le suma un punto al usuario
                            if not balloon.right:
                                BALLOON_SCORE += 1
                                SCORE += 100
                            else:
                                BALLOON_SCORE -= 1
                                SCORE -= 50
                    if balloon.rect.center[1] < -30:
                        if balloon.right:
                            BALLOON_SCORE += 1
                            SCORE += 100
                        else:
                            BALLOON_SCORE -= 1
                            SCORE -= 50
                        balloon.kill()
            clock.tick(30)
            
        if not gameover:
            level_number = level_number + 1
            BALLOON_SCORE = 0
            pygame.mixer.music.stop()
            passLevel_sound = load_sound("passLevel.wav")
            passLevel_sound.play()
    return 'loose'

def input(events,  level_number):    
    for event in events:
        global level,  balloons_group,  player,  arrow,  shoot_group,  BACKGROUND_SIZE
        
        if event.type == QUIT:
            sys.exit(0)
        elif event.type == pygame.USEREVENT + 1:
            y_pos = 675
            pos = random.randint(0, 7)
            if (pos == 3) or (pos == 4):
                y_pos = 600
            pos = 50 +100*pos
            balloon = Balloon((pos, y_pos), level.dificulty,  level.balloons_speed)
            balloon_group.add(balloon)
        elif event.type == MOUSEMOTION:
            x, y = pygame.mouse.get_pos()
            x = x - 342
            y =  y - 38
            if x > 0 and y > 0:
                player.rotate((x,  y))
            else:
                for button in button_group:
                    if button.rect.colliderect(Rect(x + 342,  y + 38,  1,  1)):
                        if not button.selected:
                            button.updateImage()
                    else:
                        if button.selected:
                            button.updateImage()
        elif event.type == MOUSEBUTTONDOWN:
            x, y = pygame.mouse.get_pos()
            for button in button_group:
                if button.rect.colliderect(Rect(x, y, 1, 1)):
                    if button.text == "play":
                        return "reset"
                    else:
                        return "exit"
            x = x - 342
            y =  y - 38
            if x > 0 and y > 0:
                shooting_sound = load_sound("shoot.wav")
                shoot = Shoot(player, level.shoot_speed,  BACKGROUND_SIZE.size[0])
                shoot_group.add(shoot)
                shooting_sound.play()
    return "nothing"
